﻿

$(function () {
    //$("#refresh_captcha").click();
    randomString();
    initValidate();
    
    $('#submit').click(function () {
        $.ajax({
            url: "/Home/checkExist",
            type: "POST",
            data: { email: $.trim($("#_email").val()) }            
        }).done(function (msg) {
            if (!$("#commentForm").valid()) {
                return;
            }
            if (msg == "0") {
                alert("Email has already existed!");
                return;
            }
            register();
        });       
    });
});

var result = '';

var AccType = new breeze.EntityQuery().from("Accounts");
manager.executeQuery(AccType).fail(function (e) { alert(e); });

vm.RoleType = ko.observableArray();

var RoleType = new breeze.EntityQuery().from("Roles");
manager.executeQuery(RoleType).then(function (data) {
    data.results.forEach(function (item) {
        vm.RoleType.push(item);
    })
    ko.applyBindings(vm);
}).fail(function (e) {
    alert(e);
});


function register() {
    var email = $.trim($('#_email').val());
    var pw = $('#inputPassword').val();
    var acctype = $('input[name=acctype]:checked', '#acctype').val();
    var status ;
    if ($('input[name=acctype]:checked', '#acctype')[0].nextElementSibling.textContent == "Employer") {
        status = 0;
    } else {
        status = 1;
    }

    var personal = manager.createEntity("PersonalInfor", { Email: email});
    var account = manager.createEntity("Account", { Email: email, Password: pw, RoleId: acctype, PersonalInfor: personal, Status: status, CVName: 'None' });
    var arr = Array();
    arr.push(personal);
    arr.push(account);

    manager.saveChanges(arr).then(function () {
        alert("Successful");
        location.href = "/Home/Login";
    }).fail(saveFailed);
    
}
function randomString() {

    // Genrate random string

    var randomstring = '';

    var chars = "0123456789ABCDEFGHIJKL!@#$%^&*()MNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";

    var string_length = 5;  //Length of Captcha

    for (var i = 0; i < string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum, rnum + 1);
    }

    // Getting canvas variable 
    var tCtx = document.getElementById('textCanvas').getContext('2d'),
    imageElem = document.getElementById('image');



    tCtx.font = "normal 24px Verdana";  //set text fonts option for  canvas,html5 canvas: Text Option

    tCtx.strokeStyle = "#000000";    //html5 canvas: Text Option

    tCtx.clearRect(0, 0, 100, 40);        //Clear previous Canvas for redraw our captcha

    tCtx.strokeText(randomstring, 30, 20, 70);  //Stroke random string to canvas

    tCtx.textBaseline = "middle";                  //html5 canvas: Text Option,line in middle of text

    imageElem.src = tCtx.canvas.toDataURL(); // provide canvas url to image src

    //console.log(imageElem.src);  //image 
    $('#_catpcha').val(randomstring);
}
function initValidate() {
    $("#commentForm").validate({
        rules: {
            email: {
                required: true,
                email: true
            },
            password: {
                minlength: 5,
                required: true,
            },
            password_confirm: {
                minlength: 5,
                required: true,
                equalTo: "#inputPassword"
            }
            ,
            catpcha: {
                equalTo: "#_catpcha"
            }
        },
        errorClass: 'errorClass',
        messages: {
            email: {
                required: "Email is required",
                email: "Proper email format is: something@example.com"
            },
            password: {
                required: "Password is required"
            },
            password_confirm: {
                required: "Password is required",
                equalTo: "Confirm password does not match"
            }
            ,
            catpcha: {
                equalTo: "Captcha does not match!"
            }
        }
    });
}